﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MShop.Core.Domain;
using MShop.Core.DataAccess;
 

namespace MShop.Web.Areas.Admin.Controllers
{
    public class DistributionController : SecureController
    {
   
        private readonly ICommonDao<Distribution> _Commondao;
        public DistributionController(ICommonDao<Distribution> commondao)
        {
            _Commondao = commondao;
        }
        
        public ActionResult Index()
        {
            return View(_Commondao.GetAll());
        }
        public ActionResult Create()
        {
           
            return View(new Distribution());
        }

        [HttpPost]
        public ActionResult Create(Distribution dist)
        {
            
            if (ModelState.IsValid)
            {
                _Commondao.Save(dist);
                _Commondao.SaveChanges();
                return RedirectToAction("index");
            }
            return View(dist);
        }

        public ActionResult Edit(int id)
        {

            return View(_Commondao.GetById(id));
        }


        public ActionResult Delete(int id)
        {
            var dist = _Commondao.GetById(id);
            _Commondao.Delete(dist);
            _Commondao.SaveChanges();
            return RedirectToAction("index");
        }


        [HttpPost]
        public ActionResult Edit(int id, Distribution dist)
        { 
            if (ModelState.IsValid)
            {
                var dbdist = _Commondao.GetById(id);
                UpdateModel<Distribution>(dbdist, new string[] { "Name", "Price", "Desc","Cod","Logo" });
                _Commondao.SaveChanges();
                return RedirectToAction("index");
            }
            return View(dist);

            
        }
    }
}
